package com.example.springboot.mapper;

import com.example.springboot.entity.UserSearchRemember;
import com.example.springboot.entity.vo.UserSearchRememberVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserSearchRememberMapper {

    @Insert("INSERT INTO user_search_remember (user_id, search_content,createdAt,updatedAt) VALUES (#{userId}, #{searchContent},${createdAt},#{updatedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(UserSearchRemember userSearchRemember);

    @Delete("DELETE FROM user_search_remember WHERE id = #{id}")
    int deleteById(int id);

    @Update("UPDATE user_search_remember SET user_id = #{userId}, search_content = #{searchContent} WHERE id = #{id}")
    int update(UserSearchRemember userSearchRemember);

    @Select("SELECT * FROM user_search_remember WHERE id = #{id}")
    UserSearchRemember findById(int id);

    @Select("SELECT DISTINCT search_content, user_id, id FROM user_search_remember WHERE user_id = #{userId} ORDER BY id DESC LIMIT 10")
    List<UserSearchRemember> findByUserId(int userId);

    @Select("SELECT search_content, COUNT(*) AS search_count FROM (SELECT search_content FROM user_search_remember ORDER BY id DESC LIMIT 10000) AS recent_searches GROUP BY search_content ORDER BY search_count DESC LIMIT 10")
    List<UserSearchRememberVo> findHotSearch();
}
